package com.lanou.sinasports.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.beans.MatchHot;
import com.lanou.sinasports.greenevents.MatHot;
import com.lanou.sinasports.utils.BitmapCache;
import com.lanou.sinasports.utils.DoubleCache;

import java.util.List;


/**
 * Created by dllo on 16/1/12.
 */
public class MatchHotAdapter extends BaseAdapter {
    private Context context;
    private List<MatchHot> data;
    private RequestQueue imgQueue;

    private ImageLoader imageLoader =new ImageLoader(imgQueue,new DoubleCache());
    private String s;

    private List<MatHot> matHots;


    public MatchHotAdapter(Context context, List<MatHot> matHots, String s) {
        this.context = context;
        this.matHots = matHots;
        this.s = s;
        imgQueue= Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(imgQueue,new BitmapCache());
    }

    public MatchHotAdapter(Context context, List<MatchHot> data) {
        this.context = context;
        this.data = data;
        imgQueue= Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(imgQueue,new BitmapCache());
        Log.d("MatchHotAdapter", "data:" + data);
    }

    @Override
    public int getCount() {

        return data != null ?data.size():matHots != null ?matHots.size():0;
    }

    @Override
    public Object getItem(int position) {

        return  data.size()>0?data.get(position):matHots.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_matchhot,parent,false);
            myViewHolder=new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
           if(data != null && !data.isEmpty()) {
               myViewHolder.tvTitle.setText(data.get(position).getDate());
               myViewHolder.tvType.setText(data.get(position).getLeagueType_cn());
               myViewHolder.tvRound.setText(data.get(position).getRound_cn());
               myViewHolder.tvName1.setText(data.get(position).getTeam1());
               myViewHolder.tvName2.setText(data.get(position).getTeam2());
               myViewHolder.tvScore1.setText(data.get(position).getScore1());
               myViewHolder.tvScore2.setText(data.get(position).getScore2());

               myViewHolder.ivLogo1.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
               myViewHolder.ivLogo1.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
               myViewHolder.ivLogo1.setImageUrl(data.get(position).getFlag1(), imageLoader);

               myViewHolder.ivLogo2.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
               myViewHolder.ivLogo2.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
               myViewHolder.ivLogo2.setImageUrl(data.get(position).getFlag2(), imageLoader);
           }else {
               myViewHolder.tvTitle.setText(matHots.get(position).getDate());
               myViewHolder.tvType.setText(matHots.get(position).getTitle());
               myViewHolder.tvRound.setText(matHots.get(position).getRound());
               myViewHolder.tvName1.setText(matHots.get(position).getTeam1());
               myViewHolder.tvName2.setText(matHots.get(position).getTeam2());
               myViewHolder.tvScore1.setText(matHots.get(position).getScore1());
               myViewHolder.tvScore2.setText(matHots.get(position).getScore2());

               myViewHolder.ivLogo1.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
               myViewHolder.ivLogo1.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
               myViewHolder.ivLogo1.setImageUrl(matHots.get(position).getFlag1(), imageLoader);

               myViewHolder.ivLogo2.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
               myViewHolder.ivLogo2.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
               myViewHolder.ivLogo2.setImageUrl(matHots.get(position).getFlag2(), imageLoader);
           }
        return convertView;
    }

    class MyViewHolder{
        TextView tvTitle;
        TextView tvType;
        TextView tvRound;
        TextView tvName1;
        TextView tvName2;
        TextView tvScore1;
        TextView tvScore2;
        NetworkImageView ivLogo1;
        NetworkImageView ivLogo2;

        public MyViewHolder(View view) {
            tvTitle= (TextView) view.findViewById(R.id.tv_matchhot_title);
            tvType= (TextView) view.findViewById(R.id.tv_matchhot_type);
            tvRound= (TextView) view.findViewById(R.id.tv_matchhot_round);
            tvName1= (TextView) view.findViewById(R.id.tv_matchhot_team1_name);
            tvName2= (TextView) view.findViewById(R.id.tv_matchhot_team2_name);
            tvScore1= (TextView) view.findViewById(R.id.tv_matchhot_team1_score);
            tvScore2= (TextView) view.findViewById(R.id.tv_matchhot_team2_score);
            ivLogo1= (NetworkImageView) view.findViewById(R.id.iv_matchhot_team1_logo);
            ivLogo2= (NetworkImageView) view.findViewById(R.id.iv_matchhot_team2_logo);

        }
    }
}
